package com.galeno.line;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/7/2520:46
 */
public class Line {
    //输出的数据泛型 K点  V点出现的次数1
    static class LineMapper extends Mapper<LongWritable, Text,IntWritable, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //线段转换成点
            try {
                String[] split = value.toString().split(",");
                int x1 = Integer.parseInt(split[0]);
                int x2 = Integer.parseInt(split[1]);
                for (int i = x1; i <=x2; i++) {
                    context.write(new IntWritable(i),new IntWritable(1));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  static   class LineReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable>{
        @Override
        protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count=0;
            for (IntWritable value : values) {
                count++;
            }
            context.write(key,new IntWritable(count));
            
            
            
        }
    }



    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf=new Configuration();
        conf.set("fs.defaultFS","file:/");
        Job job=Job.getInstance(conf,"Line");
        job.setMapperClass(LineMapper.class);
        job.setReducerClass(LineReducer.class);

        job.setMapOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(IntWritable.class);
        FileInputFormat.setInputPaths(job,new Path("D:/mrdata/line/input"));
        FileOutputFormat.setOutputPath(job,new Path("D:/mrdata/line/omg"));

        job.waitForCompletion(true);









    }

}
